const Topic = require('../modules/topic')

class TopicCls {
    async find(ctx) {
        ctx.body = await Topic.find()
    }

    async findById(ctx) {
        const {
            fields = ''
        } = ctx.query
        const selectStr = fields.split(';').filter(f => f).map(f => "+" + f).join('')
        const topic = await Topic.findById(ctx.params.id).select(selectStr)
        ctx.body = topic
    }

    async create(ctx) {
        ctx.verifyParams({
            name: {
                type: 'string',
                required: true
            },
            avatat_url: {
                type: 'string',
                required: false
            },
            introduction: {
                type: 'string',
                required: false
            }
        })
        const topic = await new Topic(ctx.request.body).save()
        ctx.body = topic
    }

    async update(ctx) {
        ctx.verifyParams({
            name: {
                type: 'string',
                required: false
            },
            avatat_url: {
                type: 'string',
                required: false
            },
            introduction: {
                type: 'string',
                required: false
            }
        })
        const topic = await Topic.findByIdAndUpdate(ctx.params.id, ctx.request.body)
        ctx.body = topic
    }

}

module.exports = new TopicCls()